﻿#include "img_conv.h"

/**
yyyy yyyy
uv uv
->
yyyy yyyy
uu
vv
*/
void yuv422sp_to_yuv422p(unsigned char* yuv422sp, unsigned char* yuv422p, int width, int height)
{
    int i, j;
    int y_size;
    int uv_size;
    unsigned char* p_y1;
    unsigned char* p_uv;

    unsigned char* p_y2;
    unsigned char* p_u;
    unsigned char* p_v;

    y_size = uv_size = width * height;

    p_y1 = yuv422sp;
    p_uv = yuv422sp + y_size;

    p_y2 = yuv422p;
    p_u = yuv422p + y_size;
    p_v = p_u + width * height / 2;

    memcpy(p_y2, p_y1, y_size);

    for (j = 0, i = 0; j < uv_size; j+=2, i++)
    {
        p_u[i] = p_uv[j];
        p_v[i] = p_uv[j+1];
    }
}

/**
yyyy yyyy
uv uv
->
yyyy yyyy
uu
vv
*/
void yuv420sp_to_yuv420p(unsigned char* yuv420sp, unsigned char* yuv420p, int width, int height)
{
    int i, j;
    int y_size = width * height;

    unsigned char* y = yuv420sp;
    unsigned char* uv = yuv420sp + y_size;

    unsigned char* y_tmp = yuv420p;
    unsigned char* u_tmp = yuv420p + y_size;
    unsigned char* v_tmp = yuv420p + y_size * 5 / 4;

    // y
    memcpy(y_tmp, y, y_size);

    // u
    for (j = 0, i = 0; j < y_size/2; j+=2, i++)
    {
        u_tmp[i] = uv[j];
        v_tmp[i] = uv[j+1];
    }
}



void yuv420p_to_yuv420sp(unsigned char* yuv420p , unsigned char* yuv420sp, int width, int height)
{
    int i, j;
    int y_size = width * height;



    unsigned char* y_tmp = yuv420p;
    unsigned char* u_tmp = yuv420p + y_size;
    unsigned char* v_tmp = yuv420p + y_size * 5 / 4;

    unsigned char* y = yuv420sp;
    unsigned char* uv = yuv420sp + y_size;

    // y
    memcpy(y,y_tmp,y_size);

    // u
    for (j = 0, i = 0; j < y_size/2; j+=2, i++)
    {
        uv[j]   = u_tmp[i];
        uv[j+1] = v_tmp[i];
    }
}



///////////////////////////////////////////////////////////
void  rgaFmtConvert(int SrcWidth,int SrcHeight,int SrcFmt,uint8_t* pSrcData,
                    int DstWidth,int DstHeight,int DstFmt,uint8_t* pDstData)
{


     IM_STATUS 		STATUS;
     rga_buffer_t 	src;
     rga_buffer_t 	dst;
     rga_buffer_handle_t src_handle, dst_handle;

     im_handle_param_t param;
     param.width  = SrcWidth;
     param.height = SrcHeight;
     param.format = SrcFmt;


     src_handle = importbuffer_virtualaddr(pSrcData, &param);

     param.width = DstWidth;
     param.height = DstHeight;
     param.format = DstFmt;
     dst_handle = importbuffer_virtualaddr(pDstData, &param);
     if (src_handle == 0 || dst_handle == 0) {
            printf("importbuffer failed!\n");
            return ;
     }

     src = wrapbuffer_handle(src_handle, SrcWidth, SrcHeight, SrcFmt );
     dst = wrapbuffer_handle(dst_handle, DstWidth, DstHeight, DstFmt);

     STATUS = imcvtcolor(src, dst,SrcFmt,DstFmt,0);

     releasebuffer_handle(src_handle);
     releasebuffer_handle(dst_handle);


}



///////////////////////////////////////////////////////////
void  rgaFmtConvertFlip(int SrcWidth,int SrcHeight,int SrcFmt,uint8_t* pSrcData,
                    int DstWidth,int DstHeight,int DstFmt,uint8_t* pDstData,
                        uint8_t* pRGBBuff)
{


     IM_STATUS 		STATUS;
     rga_buffer_t 	src;
     rga_buffer_t   flip;
     rga_buffer_t 	dst;
     rga_buffer_handle_t src_handle, flip_handle, dst_handle;

     im_handle_param_t param;
     param.width  = SrcWidth;
     param.height = SrcHeight;
     param.format = SrcFmt;

     src_handle = importbuffer_virtualaddr(pSrcData, &param);


     param.width  = SrcWidth;
     param.height = SrcHeight;
     param.format = SrcFmt;

     flip_handle = importbuffer_virtualaddr(pRGBBuff, &param);




     param.width = DstWidth;
     param.height = DstHeight;
     param.format = DstFmt;
     dst_handle = importbuffer_virtualaddr(pDstData, &param);
     if (src_handle == 0 || flip_handle == 0 ||dst_handle == 0) {
            printf("importbuffer failed!\n");
            return ;
     }

     src = wrapbuffer_handle(src_handle,  SrcWidth, SrcHeight, SrcFmt );
     flip= wrapbuffer_handle(flip_handle, SrcWidth, SrcHeight, SrcFmt );
     dst = wrapbuffer_handle(dst_handle,  DstWidth, DstHeight, DstFmt);

     STATUS = imflip(src, flip,IM_HAL_TRANSFORM_FLIP_H_V);

     STATUS = imcvtcolor(flip, dst,SrcFmt,DstFmt,0);

     releasebuffer_handle(src_handle);
     releasebuffer_handle(flip_handle);
     releasebuffer_handle(dst_handle);


}

